package club.pchome.common.util;


import com.alibaba.fastjson.JSON;
import com.chuanglan.sms.request.SmsSendRequest;
import com.chuanglan.sms.response.SmsSendResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

@Component
public class SendSms {
	Log log = LogFactory.getLog(SendSms.class);
	
	
	/***
	 * 	短信通道
	 */
	private String SMS_USER_NAME = "N2860046";
	
	private String SMS_USER_PASS = "cT0bwjdor";
	
	private String SMS_SIREVICE_URL = "http://smssh1.253.com/msg/send/json";
	
	private String SMS_SIREVICE_BATCH_URL = "http://smssh1.253.com/msg/send/json";
	
	
	/**
	 * 创蓝文化传播，短信发送方法-单发
	 * @param phone 手机号码
	 * @param content 短信内容
	 * @return
	 */
	public boolean clSendSms(String phone,String content){
		String serviceUrl = SMS_SIREVICE_URL;
		String account = SMS_USER_NAME;
		String password = SMS_USER_PASS;
		String report = "false";


		SmsSendRequest smsSingleRequest = new SmsSendRequest(account, password, content, phone,report);

		String requestJson = JSON.toJSONString(smsSingleRequest);

		System.out.println("before request string is: " + requestJson);

		String response = ChuangLanSmsUtil.sendSmsByPost(serviceUrl, requestJson);

		System.out.println("response after request result is :" + response);

		SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);

		System.out.println("response  toString is :" + smsSingleResponse);
		if(smsSingleResponse.getCode().equals("0")){
			log.info("短信发送成功,phone："+phone+"，content："+content);
			return true;
		}else{
			switch (smsSingleResponse.getCode()) {
			case "101":
				log.info("无此用户");
				break;
			case "102":
				log.info("密码错");
				break;
			case "103":
				log.info("提交过快（提交速度超过流速限制）");
				break;
			default:
				log.info("短信发送失败-反正是错了，别问我为什么");
				break;
			}	
			return false;
		}

	}

}
